# 09/10/2022 ;
library(brms)
library(rstan)
library(dplyr)

options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)

#-------------------------------------------------------------------------------
# LOAD AND MERGE COMPARATIVE DATASET ;
#-------------------------------------------------------------------------------
# ESS COMPARATIVE SURVEY DATA ;
rm(list=ls())
load("DATA_ESS.RData")

#-------------------------------------------------------------------------------
# THREE INTERACTIVE MULTILEVEL MODELS ;
#-------------------------------------------------------------------------------
bprior <- c(prior_string("normal(0,10)", class = "b"))

rc_pay <- brm(sanction_pay_no ~ vignette * ( 
                age + age2 + female + edu_bmask + income +
                cont_unemp + prev_unemp + subj_unemp + pplfair + ppltrst + pplhlp + 
                lrscale + sbbsntx + sbeqsoc + sblazy + sblwcoa + sbprvpv + sbstrec) +
                (1+vignette|cntry), data=ess, prior=bprior,
              chains=4, iter=5000, seed=1234)
summary(rc_pay)

rc_edu <- brm(sanction_edu_no ~ vignette * (
                age + age2 + female + edu_bmask + income +
                cont_unemp + prev_unemp + subj_unemp + pplfair + ppltrst + pplhlp + 
                lrscale + sbbsntx + sbeqsoc + sblazy + sblwcoa + sbprvpv + sbstrec) + 
                (1+vignette|cntry), data=ess, prior=bprior,
              chains=4, iter=5000, seed=1234)
summary(rc_edu)

rc_unp <- brm(sanction_unp_no ~ vignette * (
                age + age2 + female + edu_bmask + income +
                cont_unemp + prev_unemp + subj_unemp + pplfair + ppltrst + pplhlp + 
                lrscale + sbbsntx + sbeqsoc + sblazy + sblwcoa + sbprvpv + sbstrec) + 
                (1+vignette|cntry), data=ess, prior=bprior,
              chains=4, iter=5000, seed=1234)
summary(rc_unp)

#-------------------------------------------------------------------------------
# SAVE FINDINGS ;
#-------------------------------------------------------------------------------
save.image("MULTI_METRIC_INT.RData")
